<template>
	<view class="xueyapage">
		<view class="box">
			<view class="itemselect">
				<view class="itemtitle">
					当前血糖
				</view>
				<view class="pickerbox">
				<input type="number" placeholder="请输入血糖" class="picker" v-model="xuetang" />
				</view>
			</view>
		</view>

		<view class="bannerbox">
			<view class="title">
				请选择测量时间段
			</view>
			<view class="listbox">
				<view class="itemlist" :class="time==6?'act':''" @click="selecttime(6)">
					空腹
				</view>
				<view class="itemlist" :class="time==7?'act':''" @click="selecttime(7)">
					早餐后2小时
				</view>
				<view class="itemlist" :class="time==8?'act':''" @click="selecttime(8)">
				  午餐前
				</view>
				<view class="itemlist" :class="time==9?'act':''" @click="selecttime(9)">
					午饭后2小时
				</view>
				<view class="itemlist" :class="time==10?'act':''" @click="selecttime(10)">
					晚餐前
				</view>
				<view class="itemlist" :class="time==11?'act':''" @click="selecttime(11)">
					晚餐后2小时
				</view>
				<view class="itemlist" :class="time==12?'act':''" @click="selecttime(12)">
					睡前
				</view>
				<view class="itemlist" :class="time==13?'act':''" @click="selecttime(13)">
					随机
				</view>
			</view>
		</view>
		<view class="title">
			营养食疗师叮嘱：完整记录某一餐的餐前和餐后血糖值更具参考意义哦~
		</view>
		<view class="addbox">
			<image v-if="imgurl" class="image" :src="'http://192.168.10.93:8989'+imgurl" mode=""></image>
			<view v-if="!imgurl" class="add"  @click="addpicture">
				<view class="fang">
					+
				</view>
				<view class="addtitle">
					添加测量照片
				</view>
			</view>
		</view>
		<view class="pagebtn" @click="tijiaoxuetang">
			提交
		</view>
	</view>
</template>

<script>
	import {echartsAdd} from '../../utils/api.js'
	export default {
		data() {
			return {
				xuetang:'',
				imgurl:'',
				list:[],
				time:''
			}
		},
		mounted() {
			for (let i = 1; i <= 15; i++) {
			  this.list.push(i);
			}
		
		},
		methods: {
			selecttime(e){
				this.time=e
			},
			tijiaoxuetang(){
				if(this.xuetang==''){
					uni.showToast({
						title: '请输入血糖',
						icon:'none',
						duration: 2000
					});
					return
			
				}
				if(this.time==''){
					uni.showToast({
						title: '请选择时段',
						icon:'none',
						duration: 2000
					});
					return
				
				}
					
				if(this.imgurl==''){
					uni.showToast({
						title: '请上传图片',
						icon:'none',
						duration: 2000
					});
					return
				
				}
				let that=this
				uni.showModal({
					title: '提示',
					content: '确定提交血糖吗？',
					success: function (res) {
						if (res.confirm) {
							console.log(that.xuetang)
						   let p={
							   phone:uni.getStorageSync('phone'),
							   dataType:that.time,
							   dataValue:that.xuetang,
							   dataurl:that.imgurl
							   
						   }
					
						   echartsAdd(p).then(res=>{
							   console.log(res,'血糖')
							   if(res.data.code==200){
								   uni.showToast({
								   	title: '血糖录入完成',
								   	icon:'none',
								   	duration: 2000
								   });
							   }
						   })
					
						   
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			
			},
				
			addpicture(){
				let that=this
				uni.chooseImage({
					sourceType: ['camera'],
					sizeType: [ 'compressed'], 
					success: (res) => {
						console.log(res)
						const fileName = res.tempFiles[0].name
						const tempFilePaths = res.tempFilePaths;
						this.uploadFilePromise(tempFilePaths[0]).then((uploadedData) => {
							
							// 这里可以使用上传返回的数据进行后续操作
							let data = JSON.parse(uploadedData)
							console.log('上传成功，返回的数据:', data);
							that.imgurl=data.fileName
							
						})
						.catch((err) => {
							console.error('上传失败:', err);
						});
					}
				});
			},
			uploadFilePromise(url) {
				uni.showLoading({
					title: '上传中'
				})
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url:'http://192.168.10.93:8989/common/uploadFile', // 仅为示例，非真实的接口地址
						
						filePath: url,
						name: "file",
				
						formData: {
							'persionId': 'znnp'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data);
							}, 1000);
						},
						fail: (err) => {
							reject(err)
						},
						complete: () => {
							uni.hideLoading()
						}
					});
				});
			},
	
			
		}
	}
</script>

<style lang="less" scoped>
	.xueyapage{
		width: 100%;
		.box{
			width: 100%;
			margin-top: 40rpx;
			background-color: white;
			box-sizing: border-box;
			padding: 20rpx;
			.itemselect{
				width: 100%;
				text-align: center;
				.itemtitle{
					font-weight: bold;
					color:#0a0a0a ;
				}
			}
			.pickerbox{
				width: 50%;
				height: 80rpx;
				border: 1rpx solid #1A4f8A;
				text-align: center;
				line-height: 80rpx;
				font-size: 36rpx;
				font-weight: bold;
				margin: auto;
				margin-top: 20rpx;
				 border-radius: 10rpx;
				 
				.picker{
					width: 100%;
					height: 100%;
				}
			}
		}
		.addbox{
			width: 100%;
			margin-top: 40rpx;
			background-color: white;
			box-sizing: border-box;
			padding: 20rpx;
			position: relative;
			height: 300rpx;
			display: flex;
			align-items: center;
			.image{
				width: 200rpx;
				height: 200rpx;
			}
			.title{
				
				font-size: 28rpx;
				color: #c1c1c1;
			}
			.add{
				width: 30%;
				
				
				text-align: center;
				.fang{
					margin: auto;
					width: 90rpx;
					height: 90rpx;
					border-radius: 20rpx;
					border: 2rpx solid #e1e1e1;
					line-height: 80rpx;
					text-align: center;
					font-size: 60rpx;
					font-weight: bold;
					color: gray;
				}
				.addtitle{
					margin: auto;
					margin-top: 10rpx;
					font-size: 30rpx;
					color: gray;
					text-align: center;
					width: 60%;
					
				}
			}
		}
		.bannerbox{
			width: 100%;
			background-color: white;
			box-sizing: border-box;
			padding: 20rpx;
	       .title{
			   text-align: center;
		   }	
			   .listbox{
				   width: 100%;
				   display: flex;
				   align-items: flex-start;
				   flex-wrap: wrap;
				   .itemlist{
					   width: 40%;
					   height: 60rpx;
					   text-align: center;
					   line-height: 60rpx;
					   background-color: #E5E5E5;
					   border-radius: 30rpx;
					   margin: 5%;
					   font-size: 30rpx;
					   font-weight: bold;
					   color: #333333;
				   }
				   .act{
					   background-color: #1A4f8A !important;
					   color: white !important;
				   }
				   
			   }
	
		
	
		}
		.title{
			width: 100%;
			box-sizing: border-box;
			padding: 50rpx;
			font-size: 26rpx;
			color: gray;
		}
		.pagebtn{
			text-align: center;
			font-weight: bold;
			color: white;
			border-radius: 20rpx;
			margin: auto;
			margin-top: 80rpx;
			width: 80%;
			height: 90rpx;
			line-height: 90rpx;
			background-color: #1A4f8A;
			
		}
	}
</style>